/*
 * Skybox.cpp
 */

#include "Skybox.h"
#include <stdio.h>

Skybox::Skybox() {
	texture = NULL;
	rot_x1 = rot_x2 = rot_z1 = rot_z2 = 0;
}

Skybox::~Skybox() {
	delete[] texture;
}

void Skybox::initializeGL()
{
	if (texture == NULL)
		texture = new GLuint[6];
	// load skybox textures
	for(int i=0; i<6; i++){
		char str[50];
		sprintf(str, "data/skybox%d.bmp", (i+1));
		buildTexture(str, texture[i]);
	}
}

void Skybox::rotateSkybox(bool firstPlayer, float rx, float rz, float milis){
	if(firstPlayer){
		rot_x1 = rot_x1 + rx*milis*0.2;
		rot_z1 = rot_z1 - rz*milis*0.2;
	}
	else {
		rot_x2 = rot_x2 + rx*milis*0.2;
		rot_z2 = rot_z2 - rz*milis*0.2;
	}
}

void Skybox::setRotation(bool firstPlayer, float rx, float rz) {
	if(firstPlayer){
		rot_x1 = rx;
		rot_z1 = rz;
	}
	else {
		rot_x2 = rx;
		rot_z2 = rz;
	}
}

void Skybox::renderGL(bool firstPlayer)
{
	// Store the current matrix
	glPushMatrix();

	// Rotate skybox to match the players camera
	if(firstPlayer){
		glRotatef(rot_x1, 1.0f, 0.0f, 0.0f);
		glRotatef(rot_z1, 0.0f, 0.0f, 1.0f);
	}
	else{
		glRotatef(rot_x2, 1.0f, 0.0f, 0.0f);
		glRotatef(rot_z2, 0.0f, 0.0f, 1.0f);
	}

	// Enable/Disable features
	glPushAttrib(GL_ENABLE_BIT);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);

	// Just in case we set all vertices to white.
	glColor4f(1,1,1,1);

	// Render the bottom quad
	glBindTexture(GL_TEXTURE_2D, texture[5]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
		glTexCoord2f(1, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
		glTexCoord2f(1, 1); glVertex3f(  0.5f,  0.5f, -0.5f );
		glTexCoord2f(0, 1); glVertex3f( -0.5f,  0.5f, -0.5f );
	glEnd();

	// Render the top quad
	glBindTexture(GL_TEXTURE_2D, texture[4]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f( -0.5f,  0.5f,  0.5f );
		glTexCoord2f(1, 0); glVertex3f(  0.5f,  0.5f,  0.5f );
		glTexCoord2f(1, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
		glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f,  0.5f );
	glEnd();

	// Render the front quad
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f( -0.5f,  0.5f, -0.5f );
		glTexCoord2f(1, 0); glVertex3f(  0.5f,  0.5f, -0.5f );
		glTexCoord2f(1, 1); glVertex3f(  0.5f,  0.5f,  0.5f );
		glTexCoord2f(0, 1); glVertex3f( -0.5f,  0.5f,  0.5f );
	glEnd();

	// Render the back quad
	glBindTexture(GL_TEXTURE_2D, texture[2]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
		glTexCoord2f(1, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
		glTexCoord2f(1, 1); glVertex3f( -0.5f, -0.5f,  0.5f );
		glTexCoord2f(0, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
	glEnd();

	// Render the left quad
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
		glTexCoord2f(1, 0); glVertex3f( -0.5f,  0.5f, -0.5f );
		glTexCoord2f(1, 1); glVertex3f( -0.5f,  0.5f,  0.5f );
		glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f,  0.5f );
	glEnd();

	// Render the right quad
	glBindTexture(GL_TEXTURE_2D, texture[3]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f(  0.5f,  0.5f, -0.5f );
		glTexCoord2f(1, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
		glTexCoord2f(1, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
		glTexCoord2f(0, 1); glVertex3f(  0.5f,  0.5f,  0.5f );
	glEnd();

	// Restore enable bits and matrix
	glBindTexture(GL_TEXTURE_2D, 0);
	glPopAttrib();
	glPopMatrix();
}
